

/*
* Copyright @ 2017 com.iflysse.trains
* crm 下午7:27:04
* All right reserved.
*
*/
	
package com.qf.dao.daoImpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.qf.dao.CrmStudentDao;
import com.qf.pojo.CrmClass;
import com.qf.pojo.CrmDepartment;
import com.qf.pojo.CrmRunoff;
import com.qf.pojo.CrmStation;
import com.qf.pojo.CrmStudent;
import com.qf.pojo.PageBean;
import com.qf.pojo.PageStudent;

/**
* @desc: crm
* @descripe:XXXX
* @author: 周锐
* @createTime: 2017年7月27日 下午7:27:04
* @history:
* @version: v1.0

*/
@Transactional(value="txManager")
public class CrmStudentDaoImpl  implements CrmStudentDao {
    private HibernateTemplate htl;
    

	
	/**
	* @param htl the htl to set
	*/
	
	public void setHtl(HibernateTemplate htl) {
	
		this.htl = htl;
	}

	@Override
	public void addCrmStudent(CrmStudent student) {

		htl.save(student);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#updateCrmStudent(com.qf.pojo.CrmStudent)
	*/

	@Override
	public void updateCrmStudent(CrmStudent student) {

		htl.update(student);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findCrmStudentbyId(java.lang.Long)
	*/

	@Override
	public CrmStudent findCrmStudentbyId(Long studentId) {

		//TODO
		return htl.get(CrmStudent.class, studentId);

	}

	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStudentByPage(int, int)
	*/

	@Override
	public  List<CrmStudent> findStudentByPage( int page,  int size) {

		return htl.execute(new HibernateCallback<List<CrmStudent> >() {

			@Override
			public List<CrmStudent>  doInHibernate(Session session) throws HibernateException {
				
				//TODO
				return session.createQuery("from CrmStudent",CrmStudent.class).setFirstResult((page-1)*size).setMaxResults(size).getResultList();
					
			}
		});
	
		

	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStudentByPage(int, int)
	*/
		
	
		
	@Override
	public int findStudentpage() {
		
		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent",CrmStudent.class).getResultList().size();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#addStation(com.qf.pojo.CrmStation)
	*/
		
	@Override
	public void addStation(CrmStation station) {
		
		htl.save(station);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#updateStation(com.qf.pojo.CrmStation)
	*/
		
	@Override
	public void updateStation(CrmStation station) {
		
		htl.update(station);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStationById(java.lang.Integer)
	*/
		
	@Override
	public CrmStation findStationById(Integer stationId) {
		
		//TODO
		return htl.get(CrmStation.class, stationId);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStationByPage(int, int)
	*/
		
	@Override
	public List<CrmStation> findStationByPage(int page, int size) {
		
		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStation",CrmStation.class).setFirstResult((page-1)*size).setMaxResults(size).getResultList();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStationCount()
	*/
		
	@Override
	public int findStationCount() {
		
		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStation",CrmStation.class).getResultList().size();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#addRunoff(com.qf.pojo.CrmRunoff)
	*/
		
	@Override
	public void addRunoff(CrmRunoff runoff) {
		
		htl.save(runoff);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findRunoffById(java.lang.Integer)
	*/
		
	@Override
	public  CrmRunoff findRunoffById(Integer runOffId) {
		
		//TODO
		return htl.get(CrmRunoff.class, runOffId);
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findRunoffByPage(int, int)
	*/
		
	@Override
	public List<CrmRunoff> findRunoffByPage(int page, int size) {
		
		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmRunoff",CrmRunoff.class).setFirstResult((page-1)*size).setMaxResults(size).getResultList();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findRunoffCount()
	*/
		
	@Override
	public int findRunoffCount() {
		
		//TODO
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmRunoff",CrmRunoff.class).getResultList().size();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStudentCountbyother(java.lang.Long)
	*/
		
	@Override
	public int findStudentCountbyother(Long classId) {
		
		CrmClass crmClass = htl.get(CrmClass.class, classId);
		 int dropoutCount=htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? and c.status=?",CrmStudent.class).setParameter(0, crmClass).setParameter(1, "退学").getResultList().size();
		 int graduateCount=htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? and c.status=?",CrmStudent.class).setParameter(0, crmClass).setParameter(1, "毕业").getResultList().size();
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? ",CrmStudent.class).setParameter(0, crmClass).getResultList().size()-dropoutCount-graduateCount;
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStationCountbyother(java.lang.Long)
	*/
		
	@Override
	public int findTransCountbyother(Long classId) {
		
		CrmClass crmClass = htl.get(CrmClass.class, classId);
		 return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? and c.status=? ",CrmStudent.class).setParameter(0, crmClass).setParameter(1, "转班").getResultList().size();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findRunoffCountbyother(java.lang.Long)
	*/
		
	@Override
	public int findAdvanceCountbyother(Long classId) {
		
		CrmClass crmClass = htl.get(CrmClass.class, classId);
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? and c.status=?",CrmStudent.class).setParameter(0, crmClass).setParameter(1, "升级").getResultList().size();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findStudentByClassId(java.lang.Long)
	*/
		
	@Override
	public List<CrmStudent> findStudentByClassId(Long classId) {
		
		CrmClass crmClass = htl.get(CrmClass.class, classId);
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? ",CrmStudent.class).setParameter(0, crmClass).getResultList();
			
	}

	
	/*
	*(non-Javadoc)
	* @see com.qf.dao.CrmStudentDao#findRunoffCountbyother(java.lang.Long)
	*/
		
	@Override
	public int findRunoffCountbyother(Long classId) {
		
		CrmClass crmClass = htl.get(CrmClass.class, classId);
		return htl.getSessionFactory().getCurrentSession().createQuery("from CrmStudent as c where c.crmClass=? and c.status=?",CrmStudent.class).setParameter(0, crmClass).setParameter(1, "退学").getResultList().size();
			
	}

}

	